#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"seqlist.h"

struct stu{
	int id;
	char *name;
};

void pri_stu(const void *data);
void pri_int(const void *data);

int cmp_int(const void *data1, const void *data2);
int main(void)
{
#if 0
	seqlist *me = NULL;
	int cap = 8;
	struct stu s[10];
	struct stu s888;

	void *p = NULL;
	int iid;

   for(int i = 0; i < 10; i++){
	   s[i].id = i + 1;
	  // snprintf(s[i].name, 20, "坤坤%d号", i + 1); 直接搞段错误
	   s[i].name = malloc(20);
	   snprintf(s[i].name, 20, "坤坤%d号", i + 1);

   }	
	//初始化顺序表 
   printf("________________________初始化___________________________________\n");
	if(seqlist_init(&me, sizeof(struct stu), cap)){
		printf("坤坤没打蓝桥初始化失败\n");
	}	
	//插入数据
	printf("————————————————————————插入数据—————————————————————————————————\n");
	for(int i = 0; i < 10; i++){
		if(seqlist_insert(me, &s[i])){
			printf("哎呦你干嘛~插入失败\n");
			break;
		}
	}
	//展示数据
	printf("_______________________展示数据______________________________________\n");
	seqlist_show(me,pri_stu);

	//查找
	printf("————————————————————————————查找——————————————————————————————————————\n");
	iid = 8;
	p = seqlist_find(me, &iid, cmp_int);
	if(p != NULL){
		printf("找到啦！挑个坤坤wu-%p\n", p);
		printf("%d %s\n", ((struct stu *)p)->id, ((struct stu *)p)->name);//地址 找到 存储的东西
	}else{
		printf("哎呦你干嘛~找不到");
	}
	//删除数据
	printf("______________________________________删除数据______________________________\n");
	iid = 6;
	if(seqlist_delete(me, &iid, cmp_int) == 0){
		printf("哔哩哔哩concraft\n");
		seqlist_show(me,pri_stu);

	}else{
		printf("计算机你太美！找不到无法删除！\n");
	}
	//修改
	printf("——————————————————————————————修改————————————————————————————\n");
	iid = 1;
	s888.id = 8848; 
	s888.name = malloc(20);
	snprintf(s888.name, 20, "坤王");

	
	if(seqlist_update(me, &iid, &s888, cmp_int) == 0){
		printf("坤山靠~ 找到鸡王\n");
		seqlist_show(me, pri_stu);
	}else{
		printf("鸡王去打篮球了\n");
	}

	//销毁
	printf("坤界受到高维公鸡~~~\n");
	seqlist_destriy(&me);
	printf("鸡王陨落，坤界不存！\n");
#endif
	
	int a;

	srand(time(NULL));
	seqlist * me;
	printf("___________________________________初始化_________________________________________\n");
	seqlist_init(&me,sizeof(int), 20);
	printf("————————————————————————————插入——————————————————————————————————————————————————————————\n");
	for(int i = 0; i < 21; i++){
		a = rand() % 100;
		if(seqlist_insert(me, &a) != 0){
			printf("插入失败！\n");
			break;
		}
	}
	printf("————————————————————————————————————————展示——————————————————————————————————————————————————\n");
	seqlist_show(me, pri_int);
	printf("\n");

	
	my_qsort(me, 0, me->nnum - 1, cmp_int);
	seqlist_show(me, pri_int);
	printf("\n");



	
	scanf("%d",&a);
	printf("%p\n", seqlist_bsearch(me, cmp_int, &a));

	
	return 0;
}

void pri_stu(const void *data){
 	struct stu * d = (struct stu *)data;
	printf("%d %s\n", d->id, d->name);
}
void pri_int(const void *data){
	int *d = (int *)data;
	printf("%d ", *d);
}

/* int cmp_int(const void *data1, const void *data2){
	struct stu *s = (struct stu*) data1;
	int * d2 = (int *)data2;
	return s->id - *d2;
}
*/
int cmp_int(const void *data1, const void *data2){
	int *d1 = (int *)data1;
	int *d2 = (int *)data2;
	return *d1 - *d2;
}
